<%@ page language="java" import="java.util.*" import="java.sql.*" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ page import="com.example.demo2.DBUtil" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>后台管理系统</title>
</head>
<link rel="stylesheet" href="css/bootstrap.css">
<body>
<style>
    .center{display: flex !important;align-content: center;justify-content: center;align-items: center;}
</style>
<div style="display: flex; justify-content: center;flex-direction: column;align-items: center;">
    <%@include file="admin_header.jsp"%>
    <div style="margin-top: 20px">
        <ul class="list-group list-group-horizontal">
            <li class="list-group-item" style="width: 150px">ID</li>
            <li class="list-group-item"style="width: 150px">管理员名</li>
            <li class="list-group-item"style="width: 150px">密码</li>
            <li class="list-group-item"style="width: 150px">操作</li>
        </ul>


        <%
            request.setCharacterEncoding("UTF-8");
            String u_id = request.getParameter("u_id");
            String u_psw = request.getParameter("u_psw");

            Connection cn = null;
            PreparedStatement ps = null;

            ResultSet rs = null;
            String sql = "";
            cn = DBUtil.getConnection();
            sql = "select * from admin";
            ps = cn.prepareStatement(sql);
            rs = ps.executeQuery();

            if (u_id != null){
                int n=0;
                sql = "UPDATE admin SET psw =\""+u_psw+"\" WHERE id=" + u_id;
//                System.out.println(sql);
                ps = cn.prepareStatement(sql);
                n= ps.executeUpdate();
                response.sendRedirect("ad_my.jsp");

            }

            while(rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("user");
                String psw = rs.getString("psw");
        %>
        <ul class="list-group list-group-horizontal-sm">
            <li class="list-group-item center"style="width: 150px;"><%=id%></li>
            <li class="list-group-item center"style="width: 150px"><%=name%></li>
            <li class="list-group-item center"style="width: 150px"><%=psw%></li>
            <li class="list-group-item center"style="width: 150px">
                <button type="button" class="btn btn-primary btn-sm"
                        data-toggle="modal" data-target="#exampleModal<%=id%>"
                        style="margin-bottom: 20px;margin-right: 5px">修改</button>
            </li>
        </ul>
        <div class="modal fade" id="exampleModal<%=id%>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel">修改</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <form action="ad_my.jsp" method="post">
                            <div class="form-group" style="display: none">
                                <label for="id">ID:</label>
                                <input type="text" value="<%=id%>" name="u_id" required class="form-control"
                                       id="id">
                            </div>
                            <div class="form-group">
                                <label for="password">密码:</label>
                                <input type="password" value="<%=psw%>" name="u_psw" required class="form-control"
                                       id="password">
                            </div>

                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                                <button type="submit" class="btn btn-primary">确认</button>
                            </div>
                        </form>
                    </div>

                </div>
            </div>
        </div>

        <%
            }
        %>
    </div>

</div>
<script src="js/jQuery.js"></script>
<script src="js/bootstrap.js"></script>

</body>
</html>